package com.gem.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gem.entity.Product;
import com.gem.entity.ProductCategory;
import com.gem.vo.ProductVO;
import com.gem.mapper.ProductCategoryMapper;
import com.gem.mapper.ProductMapper;
import com.gem.service.ProductCategoryService;
import com.gem.vo.ProductCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘聪
 * @since 2020-03-22
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<ProductCategoryVO> getAllProductCategoryVO() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("sort_order",2);
        List<ProductCategory> selectList = (List<ProductCategory>)productCategoryMapper.selectList(wrapper);
        List<ProductCategoryVO> categoryVOList = selectList.stream().map(e -> new ProductCategoryVO(e.getId(), e.getName())).collect(Collectors.toList());
        for (int i = 0; i < categoryVOList.size(); i++) {
            categoryVOList.get(i).setBannerImg("/images/banner"+i+".png");
            wrapper = new QueryWrapper();
            wrapper.eq("category_id",categoryVOList.get(i).getId());
            List<Product> productList = productMapper.selectList(wrapper);
            List<ProductVO> productVOList = productList.stream()
                    .map(e -> new ProductVO(
                            e.getId(),
                            e.getName(),
                            e.getPrice(),
                            e.getStock(),
                            e.getSubImages()
                    )).collect(Collectors.toList());
            categoryVOList.get(i).setProductVOList(productVOList);
        }
        for (ProductCategoryVO categoryVOList1 : categoryVOList) {
            Integer id = categoryVOList1.getId();
            wrapper = new QueryWrapper();
            wrapper.eq("parent_id", id);
            wrapper.eq("sort_order", 3);
            List<ProductCategory> listCategory = (List<ProductCategory>)productCategoryMapper.selectList(wrapper);
            List<ProductCategoryVO> levelTwoVO = listCategory.stream().map(e -> new ProductCategoryVO(
                    e.getId(),
                    e.getName())).collect(Collectors.toList());
            categoryVOList1.setChildren(levelTwoVO);
//            categoryVOList1.getChildren().forEach(System.out::println);
//            listProduct.forEach(System.out::println);
        }
        return categoryVOList;
    }
}
